﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace Pub
{
    /*
    private void button1_Click(object sender, EventArgs e)
    {
        DxComObject javascript = new DxComObject("ScriptControl");
        //设置属性
        javascript["Language"] = "JavaScript";
        object[] args = new object[1];//指定参数
        args[0] = "1 + 1 + 3 * 2 + ((2 + 3) * 2) / 4";
        //调用方法
        string s = javascript.DoMethod("Eval", args).ToString();
        button1.Text = s;
    }
    */

    /// <summary>
    /// COM对象的后期绑定调用类库    
    /// </summary>
    public class CDxComObject
    {
        private System.Type _ObjType;
        private object ComInstance;
        /*public DxComObject()
        {
            throw new
        }*/
        public CDxComObject(string ComName)
        {
            //根据COM对象的名称创建COM对象
            _ObjType = System.Type.GetTypeFromProgID(ComName);
            if (_ObjType == null)
                throw new Exception("指定的COM对象名称无效");
            ComInstance = System.Activator.CreateInstance(_ObjType);
        }
        public System.Type ComType
        {
            get { return _ObjType; }
        }
        //执行的函数
        public object DoMethod(string MethodName, object[] args)
        {
            return ComType.InvokeMember(MethodName, System.Reflection.BindingFlags.InvokeMethod, null, ComInstance, args);
        }

        public object DoMethod(string MethodName, object[] args, System.Reflection.ParameterModifier[] ParamMods)
        {
            return ComType.InvokeMember(MethodName, System.Reflection.BindingFlags.InvokeMethod, null, ComInstance, args, ParamMods, null, null);
        }
        //获得属性与设置属性
        public object this[string propName]
        {
            get
            {
                return _ObjType.InvokeMember(propName, System.Reflection.BindingFlags.GetProperty, null, ComInstance, null);
            }
            set
            {
                _ObjType.InvokeMember(propName, System.Reflection.BindingFlags.SetProperty, null, ComInstance, new object[] { value });
            }
        }
    }

    public static class CExp
    {
        public static CDxComObject javascript = new CDxComObject("ScriptControl");

        public static string getVal(string sExp)
        {
            try
            {
                javascript["Language"] = "JavaScript";
                object[] args = new object[1];//指定参数
                args[0] = sExp;
                //调用方法
                return javascript.DoMethod("Eval", args).ToString();
            }
            catch (System.Exception ex)
            {
                throw ex;
                /*
                CDebug.Send("计算表达式出错：[" + sExp + "] " + ex.ToString());
                CDebug.Send("计算表达式出错", ex);
                return "";
                 * */
            }

        }

    }
}
